/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * clientesLista.java
 *
 * Created on 21/10/2010, 01:15:02 AM
 */
package pe.edu.upla.lamerced.desk.view.clientes;

import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pe.edu.upla.lamerced.Inicial;
import pe.edu.upla.lamerced.bean.Users;
import pe.edu.upla.lamerced.service.UsuarioService;

/**
 *
 * @author elvis
 */
public class ClientesLista extends javax.swing.JInternalFrame {

   private UsuarioService usuarioService;
   private JDesktopPane escritorio;
   private List<Users> listausuarios;

   public JDesktopPane getEscritorio() {
      return escritorio;
   }
   public void setEscritorio(JDesktopPane escritorio) {
      this.escritorio = escritorio;
   }

   /** Creates new form clientesLista */
   public ClientesLista() {
      initComponents();
   }

   /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
   @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tbClientesLista = new javax.swing.JTable();
        btnNuevoCliente = new javax.swing.JButton();
        btnEditarCliente = new javax.swing.JButton();
        btnEliminarCliente = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();

        setClosable(true);
        setTitle("Lista de clientes");

        jScrollPane1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        tbClientesLista.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null}
            },
            new String [] {
                "Nombre", "Paterno", "Materno", "Usuario", "Documento"
            }
        ));
        jScrollPane1.setViewportView(tbClientesLista);

        btnNuevoCliente.setText("Nuevo");
        btnNuevoCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoClienteActionPerformed(evt);
            }
        });

        btnEditarCliente.setText("Editar");
        btnEditarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarClienteActionPerformed(evt);
            }
        });

        btnEliminarCliente.setText("Eliminar");
        btnEliminarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarClienteActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Arial", 3, 18));
        jLabel4.setText("Lista de Clientes");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(59, 59, 59)
                        .addComponent(btnNuevoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(52, 52, 52)
                        .addComponent(btnEditarCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(49, 49, 49)
                        .addComponent(btnEliminarCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(161, 161, 161)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNuevoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEliminarCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEditarCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnEditarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarClienteActionPerformed
      if (tbClientesLista.getSelectedRow()==-1){
          JOptionPane.showMessageDialog(this, "No ha seleccionado ningun Usuario para modificar");
         return;
       }
       ClientesDatos form = new ClientesDatos();
       form.setUsuario(listausuarios.get(tbClientesLista.getSelectedRow()));
       form.cargainicial();
       form.getTxtusername().setEnabled(false);
       form.setEscritorio(escritorio);
       this.escritorio.add(form);
       form.setVisible(true);
       this.dispose();
    }//GEN-LAST:event_btnEditarClienteActionPerformed

    private void btnEliminarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarClienteActionPerformed
           if (tbClientesLista.getSelectedRow()==-1){
         JOptionPane.showMessageDialog(this, "No ha seleccionado ningun cliente");
         return;
       }
       int res = JOptionPane.showConfirmDialog(this,"Seguro que desea eliminar al cliente?","Eliminar Cliente",JOptionPane.YES_NO_OPTION);
       if(res==0){
         Users dt = listausuarios.get(tbClientesLista.getSelectedRow());
         dt.setEnabled(0);
         usuarioService.modificarUsuario(dt);
         JOptionPane.showMessageDialog(this, "se ha eliminado al cliente seleccionado");
         listausuarios = usuarioService.listaClientes();
         pintagrilla();
       }
    }//GEN-LAST:event_btnEliminarClienteActionPerformed

    private void btnNuevoClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoClienteActionPerformed
       ClientesDatos form = new ClientesDatos();
       form.setEscritorio(escritorio);
       form.setUsuario(new Users());
       form.cargainicial();
       this.escritorio.add(form);
       form.setVisible(true);
       this.dispose();
    }//GEN-LAST:event_btnNuevoClienteActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEditarCliente;
    private javax.swing.JButton btnEliminarCliente;
    private javax.swing.JButton btnNuevoCliente;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbClientesLista;
    // End of variables declaration//GEN-END:variables

   public void cargaInicial(){
      if(usuarioService==null){
        usuarioService = (UsuarioService) Inicial.getFactory().getBean("usuarioService");
      }
      if(listausuarios==null){
         listausuarios = usuarioService.listaClientes();
      }
      pintagrilla();
   }
    public void pintagrilla(){
        DefaultTableModel tabla = (DefaultTableModel) tbClientesLista.getModel();
        tabla.setRowCount(0);
        for (Users examen : listausuarios) {
            Object[] fila = new Object[5];
            fila[0] = examen.getNombre();
            fila[1] = examen.getApePaterno();
            fila[2] = examen.getApeMaterno();
            fila[3] = examen.getUsername();
            fila[4] = examen.getNroDocumento();
            tabla.addRow(fila);
        }
    }

}
